<template>
	<div class="row">
		<div class="card-header col-md-12 text-center">
			<b><h4>修改密码</h4></b>
		</div>
	    <div class="form-group col-md-12 user-first">
	        <label for="name" class="control-label col-md-2 text-right mif-height">旧密码</label>
	        <div class="col-md-10">
		        <input type="password" placeholder="请输入旧密码" autofocus="autofocus" class="form-control" v-model="form.old_password">
		    </div>
	    </div>
	     <div class="form-group col-md-12">
	        <label for="name" class="control-label col-md-2 text-right mif-height">新密码</label>
	        <div class="col-md-10">
		        <input type="password" placeholder="请输入新密码" class="form-control" v-model="form.password">
		    </div>
	    </div>
	    <div class="form-group col-md-12">
	        <label for="name" class="control-label col-md-2 text-right mif-height">确认新密码</label>
	        <div class="col-md-10">
		        <input type="password" placeholder="请再次输入新密码" class="form-control" v-model="form.password_confirmation">
		    </div>
	    </div>
	    <div class="form-group col-md-12">
	    	<div class="col-md-10 col-md-offset-2">
	    		<a class="btn btn-success btn-block" @click="resetPwd">保存</a>
	    	</div>
	    </div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					old_password: '',
					password: '',
					password_confirmation: '',
				}
			}
		},
		methods: {
			filterUser() {
				if(this.form.old_password == '') {
					this.$message('请输入旧密码')
					return false
				}
				if(this.form.password == '') {
					this.$message('请输入新密码')
					return false
				}
				if(this.form.password_confirmation == '') {
					this.$message('请再次输入新密码')
					return false
				}
				if(this.form.password != this.form.password_confirmation) {
					this.$message('两次输入的密码不一致')
					return false
				}
				return true
			},
			initalForm() {
				this.form.old_password = ''
				this.form.password = ''
				this.form.password_confirmation = ''	
			},
			resetPwd() {
				if(!this.filterUser()){
					return false
				}
				this.$http.patch('/resetPwd',this.form).then((response) => {
					this.initalForm()
					this.$message('修改成功')
				}).catch((error)=> {
					if (error.response) {
						this.initalForm()
						this.$message('修改失败，原密码错误')
				    }
				})
			}
		}
	}
</script>
<style scope>
	
</style>